<template>
  <section>
    <el-form>
      <el-form-item>
        <el-button size="mini" type="primary" @click="addBtn">+</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" v-if="list">
      <el-table-column type="index"/>
      <el-table-column label="名称" prop="title"/>
      <el-table-column label="编码" prop="code"/>
      <!--<el-table-column label="业务">-->
        <!--<template slot-scope="scope">-->
          <!--<el-button size="mini" type="text" @click="linkBtn(scope.row)">关联</el-button>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="editBtn(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="delBtn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="editDialog" v-if="editDialog" :title="currBusiness.id?'编辑':'新建'" append-to-body>
      <edit-business :business="currBusiness" v-on:cancel="getDetectionBusinessList"></edit-business>
    </el-dialog>
  </section>
</template>

<script>
  import editBusiness from './editBusiness'
  import errHandler from '@/assets/errHandler.js'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "index",
    components:{editBusiness},
    data(){
      return{
        list:null,
        editDialog:false,
        currBusiness:null
      }
    },
    created(){
      this.getDetectionBusinessList()
    },
    computed: {
      ...mapGetters(['department']),
    },
    methods:{
      ...mapActions({
        fetch: 'fetch'
      }),
      addBtn(){
        this.currBusiness={title:'',code:'',tacticsIndexs:[],data:{}}
        this.editDialog=true
      },
      editBtn(row){
        console.log(row)
        let array=row.tacticsIndexs.split('|').map(Number)
        console.log(array)
        this.currBusiness={id:row.id,title:row.title,code:row.code,tacticsIndexs:array,data:{}}
        this.editDialog=true
      },
      delBtn(row){
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"delDetectionBusiness",data:{id:row.id}})
              .then(res=>{
                console.log('删除成功----------')
                this.getDetectionBusinessList()
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },
      getDetectionBusinessList(){
        this.fetch({name:"getDetectionBusinessList"})
          .then(res=>{
            console.log('检测业务列表-------',res)
            this.list=res
            this.editDialog=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
    }
  }
</script>

<style scoped>

</style>
